package com.zhj.rabbitmq.com.result;

/**
 * @description 接口返回数据封装类
 * @author tandong
 * @date 2018年7月21日 下午3:06:34
 * @Copyright: 2018 重庆琢岳网络技术 All rights reserved.
 */
public class JsonResult<T> {

	private int code;

	private String msg;

	private T data;

	public JsonResult() {

	}

	public JsonResult(int code, String msg, T data) {
		this.code = code;
		this.msg = "小豆温馨提示："+msg;
		this.data = data;
	}

	// 成功
	public static JsonResult success() {
		return success("成功",new Object());
	}

	public static JsonResult success(String msg) {
		return success(msg, new Object());
	}

	public static <T> JsonResult<T> success(T data) {
		return success(null, data);
	}

	public static <T> JsonResult<T> success(String msg, T data) {
		return new JsonResult(ResultEnum.success.code, msg == null?"成功":msg, data);
	}

	// 失败
	public static <T> JsonResult<T> failure() {
		return failure(ResultEnum.failure.msg);
	}

	public static <T> JsonResult<T> failure(String msg) {
		return new JsonResult(ResultEnum.failure.code, msg, null);
	}

	public static <T> JsonResult<T> empty() {
		return new JsonResult(ResultEnum.empty.code, "", null);
	}

	public static <T> JsonResult<T> failure(ResultEnum re) {
		return new JsonResult(re.code, re.msg, null);
	}

	// 异常
	public static <T> JsonResult<T> exception() {
		return new JsonResult(ResultEnum.exception.code, ResultEnum.exception.msg, null);
	}

	public static <T> JsonResult<T> noLogin() {
		return new JsonResult(ResultEnum.noLogin.code, ResultEnum.noLogin.msg, null);
	}

	public boolean isSuccess(){
		return code == CommonResponse.SystemResp.BUSS_SUCC.Code;
	}
	/**
	 * @description 验证错误和不为空 带返回值
	 * @author zd
	 * @date  2019/6/20  16:33
	 * @return 数据
	 */
	public T validateAndNotNull(CommonResponse.Response response) {
		if(!this.isSuccess() || this.getData()== null){
			throw new RuntimeException(response.Msg);
		}
		return this.getData();
	}

	// 返回结果枚举
	public enum ResultEnum {
		success(0, "操作成功"),
		failure(1, "操作失败"),
		exception(-1, "系统太忙了~别急，拨4001717170找客服帮忙"),
		noLogin(2, "未登录"),
		empty(100, "前端自定义错误信息");

		private final int code;
		private final String msg;

		private ResultEnum(int code, String msg) {
			this.code = code;
			this.msg = msg;
		}

		public int getCode() {
			return code;
		}

		public String getMsg() {
			return msg;
		}

	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

}
